/*     */ package com.cr.framework.shiro.session;
/*     */ 
/*     */ import com.cr.common.enums.OnlineStatus;
/*     */ import com.cr.framework.manager.AsyncManager;
/*     */ import com.cr.framework.manager.factory.AsyncFactory;
/*     */ import com.cr.framework.shiro.service.SysShiroService;
/*     */ import java.io.Serializable;
/*     */ import java.util.Date;
/*     */ import org.apache.shiro.session.Session;
/*     */ import org.apache.shiro.session.UnknownSessionException;
/*     */ import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.beans.factory.annotation.Value;
/*     */ 
/*     */ public class OnlineSessionDAO extends EnterpriseCacheSessionDAO {
/*     */   @Value("${shiro.session.dbSyncPeriod}")
/*     */   private int dbSyncPeriod;
/*     */   
/*  31 */   private static final String LAST_SYNC_DB_TIMESTAMP = OnlineSessionDAO.class.getName() + "LAST_SYNC_DB_TIMESTAMP";
/*     */   
/*     */   @Autowired
/*     */   private SysShiroService sysShiroService;
/*     */   
/*     */   public OnlineSessionDAO() {}
/*     */   
/*     */   public OnlineSessionDAO(long expireTime) {}
/*     */   
/*     */   protected Session doReadSession(Serializable sessionId) {
/*  55 */     return this.sysShiroService.getSession(sessionId);
/*     */   }
/*     */   
/*     */   public void update(Session session) throws UnknownSessionException {
/*  61 */     super.update(session);
/*     */   }
/*     */   
/*     */   public void syncToDb(OnlineSession onlineSession) {
/*  69 */     Date lastSyncTimestamp = (Date)onlineSession.getAttribute(LAST_SYNC_DB_TIMESTAMP);
/*  70 */     if (lastSyncTimestamp != null) {
/*  72 */       boolean needSync = true;
/*  73 */       long deltaTime = onlineSession.getLastAccessTime().getTime() - lastSyncTimestamp.getTime();
/*  74 */       if (deltaTime < (this.dbSyncPeriod * 60 * 1000))
/*  77 */         needSync = false; 
/*  80 */       boolean isGuest = (onlineSession.getUserId() == null || onlineSession.getUserId().longValue() == 0L);
/*  83 */       if (!isGuest && onlineSession.isAttributeChanged())
/*  85 */         needSync = true; 
/*  88 */       if (!needSync)
/*     */         return; 
/*     */     } 
/*  94 */     onlineSession.setAttribute(LAST_SYNC_DB_TIMESTAMP, onlineSession.getLastAccessTime());
/*  96 */     if (onlineSession.isAttributeChanged())
/*  98 */       onlineSession.resetAttributeChanged(); 
/* 100 */     AsyncManager.me().execute(AsyncFactory.syncSessionToDb(onlineSession));
/*     */   }
/*     */   
/*     */   protected void doDelete(Session session) {
/* 109 */     OnlineSession onlineSession = (OnlineSession)session;
/* 110 */     if (null == onlineSession)
/*     */       return; 
/* 114 */     onlineSession.setStatus(OnlineStatus.off_line);
/* 115 */     this.sysShiroService.deleteSession(onlineSession);
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-framework-4.7.0.jar!\com\cr\framework\shiro\session\OnlineSessionDAO.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */